package com.bestcem.xm.award.controller.convert;

import com.alibaba.fastjson.JSON;
import com.bestcem.xm.award.controller.dto.draw.DrawAwardDTO;
import com.bestcem.xm.award.controller.param.draw.VerifySmsVerificationCodeParam;
import com.bestcem.xm.award.controller.vo.draw.*;
import com.bestcem.xm.award.entity.pojo.AwardAwardDO;
import com.bestcem.xm.award.service.bo.award.AwardAwardBO;
import com.bestcem.xm.award.service.dto.FileInfoDTO;
import com.bestcem.xm.award.service.dto.draw.RecordDTO;
import com.bestcem.xm.award.service.dto.draw.RecordDetailDTO;
import com.bestcem.xm.common.core.domain.web.PageResultDTO;
import com.bestcem.xm.common.core.uitls.DateUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.bestcem.xm.award.constant.Constants.DATE_PATTERN;

/**
 * 抽奖转换工具类
 *
 * @author ympan <yuming.pan@idiaoyan.com>
 * @version v3.0
 * @date 2021/8/19 16:49
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DrawAwardConvert {

    /**
     * 短信验证码校验 接收参数转VO
     *
     * @param verifySmsVerificationCodeParam 短信验证码校验 接收参数
     * @return 短信验证码校验 VO
     */
    public static VerifySmsVerificationCodeOuterVO verifySmsVerificationCodeParamToVO(VerifySmsVerificationCodeParam verifySmsVerificationCodeParam) {
        VerifySmsVerificationCodeVO verifySmsVerificationCodeVO =JSON.parseObject(JSON.toJSONString(verifySmsVerificationCodeParam.getAward()), VerifySmsVerificationCodeVO.class);
        verifySmsVerificationCodeVO.setPhone(verifySmsVerificationCodeParam.getPhone());
        return new VerifySmsVerificationCodeOuterVO(verifySmsVerificationCodeVO);
    }

    /**
     * 获取中奖名单中的奖项信息 DTO 转 VO
     *
     * @param pageResult DTO
     * @return VO
     */
    public static ListWinnersPageVO listWinnersConversion(PageResultDTO<RecordDTO> pageResult) {
        List<RecordDTO> recordDTOList = pageResult.getData();
        List<ListWinnersWinnerDetailsVO> list = new ArrayList<>();
        recordDTOList.forEach(recordDTO -> {
            ListWinnersWinnerDetailsVO listWinnersWinnerDetailsVO = new ListWinnersWinnerDetailsVO();
            listWinnersWinnerDetailsVO.setPhone(recordDTO.getPhone());
            listWinnersWinnerDetailsVO.setReceiveTime(recordDTO.getReceiveTime());
            listWinnersWinnerDetailsVO.setRecordId(recordDTO.getRecordId());
            listWinnersWinnerDetailsVO.setSurveySeq(recordDTO.getSurveySeq());
            listWinnersWinnerDetailsVO.setDeliverSource(recordDTO.getDeliverSource());
            listWinnersWinnerDetailsVO.setDeliverName(recordDTO.getDeliverName());
            ListWinnersInfoVO listWinnersInfoVO = new ListWinnersInfoVO();
            listWinnersInfoVO.setWinLevel(recordDTO.getWinLevel());
            listWinnersInfoVO.setWinName(recordDTO.getWinName());
            listWinnersInfoVO.setLevelStr(recordDTO.getLevelStr());
            listWinnersWinnerDetailsVO.setInfo(listWinnersInfoVO);
            ListWinnersWinnerVO listWinnersWinnerVO = new ListWinnersWinnerVO();
            listWinnersWinnerVO.setAddress(recordDTO.getAddress());
            listWinnersWinnerVO.setName(recordDTO.getName());
            listWinnersWinnerVO.setPhone(recordDTO.getPhone());
            listWinnersWinnerDetailsVO.setWinner(listWinnersWinnerVO);
            listWinnersWinnerDetailsVO.setStatus(recordDTO.getStatus());
            listWinnersWinnerDetailsVO.setCode(recordDTO.getCode());
            listWinnersWinnerDetailsVO.setReceiveTimeStr(recordDTO.getReceiveTimeStr());
            list.add(listWinnersWinnerDetailsVO);
        });
        ListWinnersPageVO listWinnersPageVO = new ListWinnersPageVO();
        listWinnersPageVO.setWinnerDetails(list);
        listWinnersPageVO.setTotal(pageResult.getTotal());
        return listWinnersPageVO;
    }

    public static ListWinnerDetailsPageVO listWinnerDetailsConversion(PageResultDTO<RecordDetailDTO> pageResult) {
        List<RecordDetailDTO> recordDetailDTOList = pageResult.getData();
        List<ListWinnerDetailsOuterVO> list = new ArrayList<>();
        recordDetailDTOList.forEach(recordDetailDTO -> {
            ListWinnerDetailsVO listWinnerDetailsVO = new ListWinnerDetailsVO();
            listWinnerDetailsVO.setLevel(recordDetailDTO.getLevel());
            listWinnerDetailsVO.setName(recordDetailDTO.getName());
            listWinnerDetailsVO.setLevelStr(recordDetailDTO.getLevelStr());
            ListWinnerDetailsOuterVO listWinnerDetailsOuterVO = new ListWinnerDetailsOuterVO();
            listWinnerDetailsOuterVO.setInfo(listWinnerDetailsVO);
            listWinnerDetailsOuterVO.setRecordId(recordDetailDTO.getRecordId());
            listWinnerDetailsOuterVO.setCode(recordDetailDTO.getCode());
            list.add(listWinnerDetailsOuterVO);
        });
        ListWinnerDetailsPageVO listWinnerDetailsPageVO = new ListWinnerDetailsPageVO();
        listWinnerDetailsPageVO.setInfo(list);
        listWinnerDetailsPageVO.setTotal(pageResult.getTotal());
        return listWinnerDetailsPageVO;
    }

    /**
     * 导出中奖名单 DTO 转 VO
     *
     * @param fileInfoDTO DTO
     * @return VO
     */
    public static ExportWinnerOuterVO exportWinnerConversion(FileInfoDTO fileInfoDTO) {
        ExportWinnerVO exportWinnerVO = new ExportWinnerVO();
        BeanUtils.copyProperties(fileInfoDTO,exportWinnerVO);
        return new ExportWinnerOuterVO(exportWinnerVO);
    }

    /**
     * 导出中奖名单中的奖项信息 DTO 转 VO
     *
     * @param fileInfoDTO DTO
     * @return VO
     */
    public static ExportWinnerDetailsVO exportWinnerDetailsConversion(FileInfoDTO fileInfoDTO) {
        ExportWinnerDetailsVO exportWinnerDetailsVO = new ExportWinnerDetailsVO();
        BeanUtils.copyProperties(fileInfoDTO,exportWinnerDetailsVO);
        return exportWinnerDetailsVO;
    }

    /**
     * 抽奖奖励数据转换
     *
     * @return {@link AwardAwardDO}
     */
    public static AwardAwardBO drawAwardDtoToAward(DrawAwardDTO dto) {
        AwardAwardBO award = new AwardAwardBO();
        award.setId(dto.getId());
        award.setName(dto.getName());
        award.setOrgId(dto.getOrgId());
        award.setProjectId(dto.getProjectId());
        award.setTType(dto.getType());
        // 奖项列表
        if (!CollectionUtils.isEmpty(dto.getDrawAwards())) {
            List<AwardAwardBO.Award> awards = new ArrayList<>();
            for (DrawAwardDTO.DrawAward drawAward : dto.getDrawAwards()) {
                AwardAwardBO.Award awardAward = new AwardAwardBO.Award();
                awardAward.setLevel(drawAward.getLevel());
                awardAward.setName(drawAward.getName());
                awardAward.setDesc(drawAward.getDesc());
                awardAward.setProb(new BigDecimal(drawAward.getProbability()));
                if (Objects.nonNull(drawAward.getNum())) {
                    awardAward.setNum(drawAward.getNum() == 0 ? null : drawAward.getNum());
                }
                awardAward.setMethod(drawAward.getMethod());
                awardAward.setPrefix(drawAward.getPrefix());
                awardAward.setTip(drawAward.getTip());
                awardAward.setLevelStr(drawAward.getLevelStr());
                awardAward.setImg(drawAward.getImgUrl());
                awards.add(awardAward);
            }
            award.setAwards(awards);
        }
        // 奖励基本配置
        if (Objects.nonNull(dto.getBasic())) {
            AwardAwardBO.Basic basic = new AwardAwardBO.Basic();
            basic.setTotalCount(dto.getBasic().getTotalCount());
            basic.setStartTime(DateUtil.getDate(cn.hutool.core.date.DateUtil.parse(dto.getBasic().getStartTime(), DATE_PATTERN)));
            basic.setStopTime(DateUtil.getDate(cn.hutool.core.date.DateUtil.parse(dto.getBasic().getEndTime(), DATE_PATTERN)));
            if (Objects.nonNull(dto.getBasic().getDisplayLogo())) {
                basic.setDisplayLogoId(dto.getBasic().getDisplayLogo().getId());
                basic.setDisplayName(dto.getBasic().getDisplayLogo().getName());
                basic.setDisplayLogoPath(dto.getBasic().getDisplayLogo().getUrl());
            }
            award.setBasic(basic);
        }
        // 发放配置
        if (Objects.nonNull(dto.getIssue())) {
            AwardAwardBO.Issue issue = new AwardAwardBO.Issue();
            issue.setSms(dto.getIssue().getSmsContent());
            issue.setMethod(dto.getIssue().getType());
            issue.setTip(dto.getIssue().getTip());
            issue.setPrefix(dto.getIssue().getPrefix());
            issue.setTemplateId(dto.getIssue().getTemplateId());
            issue.setTemplateTitle(dto.getIssue().getTemplateTitle());
            award.setIssue(issue);
        }
        // 抽奖规则配置
        if (Objects.nonNull(dto.getRule())) {
            AwardAwardBO.Rule rule = new AwardAwardBO.Rule();
            if (Objects.nonNull(dto.getRule().getDraw())) {
                rule.setDraw(dto.getRule().getDraw() == 0 ? null : dto.getRule().getDraw());
            }
            if (Objects.nonNull(dto.getRule().getWin())) {
                rule.setWin(dto.getRule().getWin() == 0 ? null : dto.getRule().getWin());
            }
            if (Objects.nonNull(dto.getRule().getTime())) {
                rule.setTime(dto.getRule().getTime() == 0 ? null : dto.getRule().getTime());
            }
            if (Objects.nonNull(dto.getRule().getScore())) {
                rule.setSscore(dto.getRule().getScore() == 0 ? null : dto.getRule().getScore());
            }
            rule.setStopAward(dto.getRule().getStopAward());
            award.setRule(rule);
        }
        return award;
    }
}
    